package com.shujia.day14;

import java.util.TreeSet;

/*
    要想使用TreeSet集合存储自定义对象且使用的是无参构造方法创建TreeSet集合对象的话，
    需要元素类型实现Comparable<元素类型>接口,实现compareTo方法
    compareTo方法根据需求来定制
 */
public class TreeSetDemo2 {
    public static void main(String[] args) {
        //使用TreeSet存储自定义对象
        TreeSet<Teacher> set1 = new TreeSet<>();
        //需求：当姓名和年龄一样的时候去重，年龄需要从小到大排序
        Teacher t1 = new Teacher("笑哥", 18);
        Teacher t2 = new Teacher("小虎", 15);
        Teacher t3 = new Teacher("凯哥", 17);
        Teacher t4 = new Teacher("笑哥", 18);
        Teacher t5 = new Teacher("杨老板", 18);


        set1.add(t1);
        set1.add(t2);
        set1.add(t3);
        set1.add(t4);
        set1.add(t5);

        for (Teacher teacher : set1) {
            System.out.println(teacher);
        }
    }
}
